var qfang={};
qfang.CkeditorConfig= {
    config: {
        toolbar: [['Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript'],
                  ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
                  ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent'], 
                  ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'], 
                  ['Link', 'Unlink'], 
                  ['Image', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar'],
                  '/', ['Styles', 'Format', 'Font', 'FontSize'],
                  ['TextColor', 'BGColor'], 
                  ['Maximize', 'ShowBlocks', '-', '-', 'Undo', 'Redo']]
    },
    init: function(){
        this.addUploadButton(CKEDITOR.instances.editor1);
    },    
    addUploadButton: function(editor){
        CKEDITOR.replace('editor1', qfang.CkeditorConfig.config);
        CKEDITOR.on('dialogDefinition', function(ev){
            // Take the dialog name and its definition from the event
            // data.
            var dialogName = ev.data.name;
            var dialogDefinition = ev.data.definition;
            
            // Check if the definition is from the dialog we're
            // interested on (the "Link" dialog).
            if (dialogName == 'image') {
            
                // Get a reference to the "Link Info" tab and the "Browse" button.
                var infoTab = dialogDefinition.getContents('info');
                var browseButton = infoTab.get('browse');
                browseButton.hidden = false;
                dialogDefinition.onLoad = function(){                
                    new AjaxUpload($(".cke_dialog_body .cke_dialog_page_contents:first .cke_dialog_ui_button:first"), {
                        action: '/fckeditorTest/index3.jsp',
                        autoSubmit: true,
                        name: "upload",
                        responseType: "json",
                        onSubmit: function(file, extension){
                            if (extension && /^(jpg|png|jpeg|gif)$/.test(extension)) {
                                //$("#loading").html('<img src="images/loading.gif">');
                                //$("#loading").show();
                                //$("#fileButton").attr("disabled","disabled");
                            }
                            else {
                                //alert("你所选择的文件不受系统支持");
                                alert("只允许上传jpg|png|jpeg|gif图片");
                                return false;
                            }
                        },
                        onComplete: function(file, json){
                            if (json.ret == -100) {
                                alert("对不起，登录后才可以上传图片");
                                // var url = mm.common.business.url;
                                //   url.showLogon(url.getLogonUrl(true, { "logonTip": "evaluation" }) + "&iframe=true&height=400&width=600", "", "");
                                return false;
                            }
                            if (json.ret == -110) {
                                alert("对不起，图片类型不支持");
                                return false;
                            }
                            if (json.ret == -120) {
                                var picture_max_size = $("#picture_max_size").val();
                                if (!picture_max_size) {
                                    picture_max_size = 1;
                                }
                                alert("对不起,图片大小不能超过" + picture_max_size + 'M');
                                return false;
                            }
                            if (json.ret == -130) {
                                var picture_max_num = $("#picture_max_num").val();
                                if (!picture_max_size) {
                                    picture_max_num = 20;
                                }
                                alert("对不起,每天上传的图片数不能超过" + picture_max_num + '张');
                                return false;
                            }
                            if (json.ret == -998) {
                                alert("请求非法");
                                return false;
                            }
                            if (json.ret != 0) {
                                alert("对不起，系统繁忙，请稍候再试！");
                                return false;
                            }
                            // 在输入框显示url，并触发url文本框的onchange事件，以便预览图片
                            var urlInputText = $(".cke_dialog_body .cke_dialog_page_contents:first input.cke_dialog_ui_input_text:first");
                            urlInputText.val(json.url);
                            if (document.all) {//IE
                                urlInputText[0].fireEvent("onchange");
                            }
                            else { //FireFox
                                var evt = document.createEvent('HTMLEvents');
                                evt.initEvent('change', true, true);
                                urlInputText[0].dispatchEvent(evt);
                            }
                        }
                    });
                    
                }
                
            }
            
        });
    }
};
qfang.CkeditorConfig.init();
